package com.ebo.kline.listener;

import android.content.Context;
import android.widget.TextView;

import com.ebo.commonlib.utils.DigitUtils;
import com.ebo.kline.KConfig;
import com.ebo.kline.KLineView;
import com.ebo.kline.model.IKData;
import com.ebo.kline.view.ChartInfoView;
import com.github.mikephil.charting.charts.Chart;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by admin on 2018/5/30.
 */

public class MainInfoViewListener extends InfoViewListener {

    private int type = KLineView.ELEMENT_K;
    private int digits = 6;

    public MainInfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView) {
        super(context, lastClose, list, infoView);
        this.digits = digits;
    }

    public MainInfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView, Chart otherChart) {
        super(context, lastClose, list, infoView, otherChart);
        this.digits = digits;
    }

    public MainInfoViewListener(Context context, double lastClose, List<IKData> list, ChartInfoView infoView, TextView descView, Chart otherChart) {
        super(context, lastClose, list, infoView, descView, otherChart);
        this.digits = digits;
    }

    private String[] colors;
    private int aveColor;

    @Override
    public void setConfig(KConfig config) {
        super.setConfig(config);
        colors = new String[config.SMA.getMACount()];
        for(int i =0 ; i< colors.length;i++){
            colors[i] = String.format("#%06X", (0xFFFFFF & config.SMA.getMA(i).getColor()));
        }
        aveColor = config.timeAveColor;
    }

    public void setType(int type){
        this.type = type;
    }

    public void setDigits(int digits){
        this.digits = digits;
    }

    @Override
    protected String getDesc(int position){
        if(type == KLineView.ELEMENT_TIME){
            return getTimeDesc(position);
        }else if(type == KLineView.ELEMENT_K){
            return getKDesc(position);
        }else
            return "";
    }


    private String getTimeDesc(int position){
        StringBuilder desc = new StringBuilder("");
        desc.append("<font color=\"");
        desc.append(aveColor);
        desc.append("\">");
        desc.append("MA");
        desc.append(20);
        desc.append(":");
        Double value = new Double(mList.get(position).getSma().getSma20());
        desc.append(Double.isNaN(value)?"--": DigitUtils.doublestr(value.floatValue(),digits));
        desc.append("</>");
        return  desc.toString();
    }

    private String getKDesc(int position){
        StringBuilder desc = new StringBuilder("");
        int i =0;
        for (LinkedHashMap.Entry<Integer, Double> entry : mList.get(position).getSma().getSma().entrySet()) {
            if(i >= mList.get(position).getMACycle().length)  break;
            desc.append("<font color=\"");
            if(i < colors.length)
                desc.append(colors[i]);
            else
                desc.append("#FFFFFF");
            desc.append("\">");
            desc.append("MA");
            desc.append(entry.getKey());
            desc.append(":");
            Double value = entry.getValue();
            desc.append(Double.isNaN(value)?"--":DigitUtils.doublestr(value.floatValue(),digits));
            desc.append("</>");
            desc.append(" ");
            i++;
        }
        return desc.toString();
    }


    public static String getOrdinaryCounting(double value) {
        BigDecimal db = new BigDecimal(value);
        String str = db.toPlainString();
        return str;
    }


}
